using HKB.Common;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace HKB.Web
{
	public class Shop_Message
	{
		private Conn conn = null;
		private Pagination _pageHand = null;
		protected Pagination pageHand
		{
			get
			{
				Pagination pageHand;
				if (this._pageHand == null)
				{
					this._pageHand = new Pagination();
					this._pageHand.SQL_CloseTablePre = false;
					this._pageHand.SQL_Fields = "*,(Select UserName From [HL_User] Where UserID=HLShop_Message.M_UserID)as UserName";
					this._pageHand.P_Object = Language.Get("ShopMessage/MessageName");
					pageHand = this._pageHand;
				}
				else
				{
					pageHand = this._pageHand;
				}
				return pageHand;
			}
		}
		public Shop_Message()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public Shop_Message(Conn _conn)
		{
			this.conn = _conn;
		}
		public void DeleteMessage(int M_ID)
		{
			this.conn.Execute("DELETE From [HLShop_Message] Where M_ID=" + M_ID);
		}
		public DataTable GetAllMessages(int indexpage, int maxperpage, string WhereStr, string OrderByStr)
		{
			return this.pageHand.GetDataTable("HLShop_Message", indexpage, maxperpage, "M_ID", WhereStr, OrderByStr, this.conn, null);
		}
		public DataTable GetAllMessages(int indexpage, int maxperpage, string WhereStr, string OrderByStr, params IDbDataParameter[] param)
		{
			return this.pageHand.GetDataTable("HLShop_Message", indexpage, maxperpage, "M_ID", WhereStr, OrderByStr, this.conn, param);
		}
		public string GetPagePanel(string LinkHead)
		{
			this.pageHand.P_LinkHead = LinkHead;
			return this.pageHand.ShowPagePanel();
		}
		public DataTable GetMessageByGetMessageByStatusContentBDateEDateProductyIDShopIDType(string M_Status, string M_Content, string M_BeginDate, string M_EndDate, int M_ProductID, int M_ShopID, int M_Type)
		{
			return null;
		}
		public bool ReplayMessage(string M_Content, int M_UserID, int M_ID, string M_SenderName)
		{
			bool result;
			if (this.GetMessageByRequestID(M_ID).Rows.Count < 1)
			{
				SqlParameter[] array = new SqlParameter[4];
				array[0] = new SqlParameter("@M_Content", DbType.Int32);
				array[0].Value = M_Content;
				array[1] = new SqlParameter("@M_UserID", DbType.Int32);
				array[1].Value = M_UserID;
				array[2] = new SqlParameter("@M_ID", DbType.String);
				array[2].Value = M_ID;
				array[3] = new SqlParameter("@M_SenderName", DbType.String);
				array[3].Value = M_SenderName;
				int num = this.conn.Execute("Pr_ReplayMessage", array);
				result = (num >= 1);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public DataTable GetMessageByShopIDLately(int ShopID, string MessageType)
		{
			DataTable dataTable;
			if (MessageType != null)
			{
				if (MessageType == "late")
				{
					dataTable = this.conn.GetDataTable("SELECT HLShop_Message_1.M_Content AS R_Content,  HLShop_Message_1.M_DateTime AS R_DateTime,   HLShop_Message_1.M_ReceiverID AS ReceiverID,   HLShop_Message_1.M_ReceiverName AS ReceiverName, HLShop_Message.* FROM HLShop_Message LEFT OUTER JOIN  HLShop_Message AS HLShop_Message_1 ON   HLShop_Message_1.M_ReplayID = HLShop_Message.M_ID WHERE (HLShop_Message.M_ShopID =" + ShopID.ToString() + ") AND   (HLShop_Message.M_Type = 1) AND (HLShop_Message.M_ReplayID = 0) AND (DATEDIFF(day,HLShop_Message.M_DateTime,getdate())<=30)");
					return dataTable;
				}
				if (MessageType == "all")
				{
					dataTable = this.conn.GetDataTable("    SELECT HLShop_Message_1.M_Content AS R_Content,  HLShop_Message_1.M_DateTime AS R_DateTime,   HLShop_Message_1.M_ReceiverID AS ReceiverID,  HLShop_Message_1.M_UserID As SenderID,  HLShop_Message_1.M_ID AS R_ID,   HLShop_Message_1.M_SenderName AS ReceiverName, HLShop_Message.*   FROM HLShop_Message LEFT OUTER JOIN   HLShop_Message AS HLShop_Message_1 ON   HLShop_Message_1.M_ReplayID = HLShop_Message.M_ID   WHERE (HLShop_Message.M_ShopID =" + ShopID.ToString() + ") AND   (HLShop_Message.M_Type = 1) AND (HLShop_Message.M_ReplayID = 0)   ORDER BY HLShop_Message.M_DateTime DESC ");
					return dataTable;
				}
			}
			dataTable = this.conn.GetDataTable("SELECT HLShop_Message_1.M_Content AS R_Content,  HLShop_Message_1.M_DateTime AS R_DateTime,   HLShop_Message_1.M_ReceiverID AS ReceiverID,   HLShop_Message_1.M_ReceiverName AS ReceiverName, HLShop_Message.* FROM HLShop_Message LEFT OUTER JOIN  HLShop_Message AS HLShop_Message_1 ON   HLShop_Message_1.M_ReplayID = HLShop_Message.M_ID WHERE (HLShop_Message.M_ShopID =" + ShopID.ToString() + ") AND   (HLShop_Message.M_Type = 1) AND (HLShop_Message.M_ReplayID = 0) AND (DATEDIFF(day,HLShop_Message.M_DateTime,getdate())<=30)");
			return dataTable;
		}
		public DataTable GetMessageByTypeUserIDSalerID(int mType, int UserID, string UserType)
		{
			SqlParameter[] array = new SqlParameter[3];
			array[0] = new SqlParameter("@Type", DbType.Int32);
			array[0].Value = mType;
			array[1] = new SqlParameter("@UserID", DbType.Int32);
			array[1].Value = UserID;
			array[2] = new SqlParameter("@UserType", DbType.String);
			array[2].Value = UserType;
			return this.conn.GetDataTable("pr_GetBuyerMessage", array);
		}
		public void InsertMessage(int M_Type, int M_ProductID, int M_ShopID, int M_UserID, bool M_Status, string M_Content, string M_SenderName, int M_ReceiverID, string M_ReceiverName, string M_UserType, int M_RequestID)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@M_ReceiverName", DbType.String, M_ReceiverName),
				this.conn.CreateParameter("@M_Type", DbType.Int32, M_Type),
				this.conn.CreateParameter("@M_ProductID", DbType.Int32, M_ProductID),
				this.conn.CreateParameter("@M_ShopID", DbType.Int32, M_ShopID),
				this.conn.CreateParameter("@M_RequestID", DbType.Int32, M_RequestID),
				this.conn.CreateParameter("@M_UserID", DbType.Int32, M_UserID),
				this.conn.CreateParameter("@M_Status", DbType.Boolean, M_Status),
				this.conn.CreateParameter("@M_Content", DbType.String, M_Content),
				this.conn.CreateParameter("@M_ReceiverID", DbType.Int32, M_ReceiverID),
				this.conn.CreateParameter("@M_SenderName", DbType.String, M_SenderName),
				this.conn.CreateParameter("@M_UserType", DbType.String, M_UserType)
			};
			this.conn.Execute("INSERT INTO [HLShop_Message] ([M_ReplayID], [M_Type], [M_ProductID], [M_ShopID], [M_DateTime], [M_UserID], [M_Status], [M_Content], [M_ReceiverID], [M_SenderName], [M_ReceiverName], [M_UserType], [M_RequestID]) VALUES (0, @M_Type, @M_ProductID, @M_ShopID, '" + DateTime.Now.ToString() + "', @M_UserID, @M_Status, @M_Content, @M_ReceiverID, @M_SenderName, @M_ReceiverName, @M_UserType, @M_RequestID)", parameters);
		}
		public DataTable GetAllMessageByShopID(int ShopID)
		{
			return this.conn.GetDataTable("Select * From [HLShop_Message] Where M_ShopID=" + ShopID);
		}
		public bool OpinionIsReplay(int M_ID)
		{
			return this.conn.ExecuteCount("Select * From [HLShop_Message] Where M_ReplayID=" + M_ID) >= 1;
		}
		public DataTable GetMessageByProductID(int ProductID)
		{
			return this.conn.GetDataTable(" SELECT HLShop_Message_1.M_Content AS R_Content, HLShop_Message_1.M_DateTime AS R_DateTime,  HLShop_Message_1.M_ReceiverID AS ReceiverID,  HLShop_Message_1.M_UserID As SenderID,  HLShop_Message_1.M_SenderName AS ReceiverName, HLShop_Message.*  FROM HLShop_Message LEFT OUTER JOIN   HLShop_Message AS HLShop_Message_1 ON   HLShop_Message_1.M_ReplayID = HLShop_Message.M_ID  WHERE (HLShop_Message.M_ProductID = " + ProductID + ") AND  (HLShop_Message.M_Type = 0) AND (HLShop_Message.M_ReplayID = 0)");
		}
		public DataTable GetMessageByRequestID(int RequestID)
		{
			return this.conn.GetDataTable("SELECT HLShop_Message_1.M_Content AS R_Content,     HLShop_Message_1.M_DateTime AS R_DateTime,     HLShop_Message_1.M_ReceiverID AS ReceiverID,     HLShop_Message_1.M_ReceiverName AS ReceiverName, HLShop_Message.* FROM HLShop_Message LEFT OUTER JOIN   HLShop_Message AS HLShop_Message_1 ON    HLShop_Message_1.M_ReplayID = HLShop_Message.M_ID WHERE (HLShop_Message.M_RequestID =" + RequestID + ") AND     (HLShop_Message.M_Type = 2) AND (HLShop_Message.M_ReplayID = 0)");
		}
		public DataTable GetMessageByMessageID(int MessageID)
		{
			return this.conn.GetDataTable("Select * From [HLShop_Message] Where M_ID=" + MessageID);
		}
		public bool UpdateMessageByMessageID(int M_ID, string M_Countent)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@M_Content", DbType.String, M_Countent)
			};
			int num = this.conn.Execute("UPDATE [HLShop_Message] Set M_Content=@M_Content Where M_ID=" + M_ID, parameters);
			return num == 1;
		}
	}
}
